{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6e20ebd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytorch_lightning as pl\n",
    "\n",
    "from pytorch_lightning import LightningDataModule, LightningModule, Trainer\n",
    "from torch.utils.data import DataLoader, random_split\n",
    "from torchvision import datasets, transforms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "205cac6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MNISTDataModule(LightningDataModule):\n",
    "    DATASET_DIR = \"datasets\"\n",
    "    \n",
    "    def __init__(self, transform=None, batch_size=100):\n",
    "        super(MNISTDataModule, self).__init__()\n",
    "        if transform is None:\n",
    "            # Default transform\n",
    "            transform = transforms.Compose([transforms.Resize((32, 32)),\n",
    "                                 transforms.ToTensor()])\n",
    "        self.transform = transform\n",
    "        self.batch_size = batch_size\n",
    "\n",
    "    \n",
    "    def prepare_data(self):\n",
    "        \"\"\"\n",
    "        All the steps needed to download, tokenize, prepare the raw data should be done under\n",
    "        prepare data. We will download the MNIST dataset here.\n",
    "        \"\"\"\n",
    "        # Download the train data\n",
    "        datasets.MNIST(root = MNISTDataModule.DATASET_DIR, train = True, download = True)\n",
    "               \n",
    "        # Download the test data\n",
    "        datasets.MNIST(root = MNISTDataModule.DATASET_DIR, train = False, download = True)\n",
    "    \n",
    "    def setup(self, stage=None):\n",
    "        \"\"\"\n",
    "        The steps to setup the dataset are usually done under setup method. \n",
    "        \"\"\"\n",
    "        train_dataset = datasets.MNIST(root = MNISTDataModule.DATASET_DIR, train = True, \n",
    "                                            download = False, transform=self.transform)\n",
    "        # We will split the train dataset into train and validation sets.\n",
    "        # All experiments are run using the train and val datasets\n",
    "        self.train_dataset, self.val_dataset = random_split(train_dataset, [55000, 5000])\n",
    "        self.test_dataset = datasets.MNIST(root = MNISTDataModule.DATASET_DIR, train = False, \n",
    "                                            download = False, transform=self.transform)\n",
    "    \n",
    "    \n",
    "    def train_dataloader(self):\n",
    "        \"\"\"\n",
    "        As evident by the name, this method is responsible for creating and returning the \n",
    "        train dataloader\n",
    "        \"\"\"\n",
    "        return DataLoader(self.train_dataset, batch_size=self.batch_size, \n",
    "                          shuffle=True, num_workers=0) \n",
    "    \n",
    "    def val_dataloader(self):\n",
    "        \"\"\"\n",
    "        As evident by the name, this method is responsible for creating and returning the \n",
    "        val dataloader\n",
    "        \"\"\"\n",
    "        return DataLoader(self.val_dataset, batch_size=self.batch_size, \n",
    "                          shuffle=False, num_workers=0) \n",
    "    \n",
    "    def test_dataloader(self):\n",
    "        \"\"\"\n",
    "        As evident by the name, this method is responsible for creating and returning the \n",
    "        val dataloader\n",
    "        \"\"\"\n",
    "        return DataLoader(self.test_dataset, batch_size=self.batch_size, \n",
    "                                          shuffle=False, num_workers=0)\n",
    "    \n",
    "    @property\n",
    "    def num_classes(self):\n",
    "        return 10"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
